home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: "Best fit" algorithm (help)
- Date: 27 Mar 1996 10:04:26 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jbvvaINNsqi@mayne.ugrad.cs.ubc.ca>
- References: <APC&7'0'22b6b83'874@peg.apc.org> <4j9215INNgbo@keats.ugrad.cs.ubc.ca> <4jbb9d$g14@news.xs4all.nl>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <4jbb9d$g14@news.xs4all.nl>, Falstaff <falstaff@xs4all.nl> wrote:
- >c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- >
- >> >I am looking for a "best fit" algorithm.
- >> >The problem I have is very similar to finding the best way of fitting the
- >> >maximum number of songs on (say) 45 minute tape.
- >
- >>Assuming that you don't care _which_ songs go on the tape, try a ``greedy''
- >>approach. Always add to the tape a song which will leave the most free space.
- >
- >>This means starting with the smallest one, and adding the next bigger one and
- >>so on. This should yield an optimal solution for the constraint of maximizing
- >>the number of songs that go on the tape. It won't minimize the left-over space,
- >>however.
- >
- >Hmmm. I think the user wants to minimize just that (unused space).
-
- The quoted paragraph clearly states an objective involving the maximum _number_
- of songs, as well as the objective of finding a good fit.
-
- >To do that, you should pick the *largest* item that will still fit.
-
- Such a greedy strategy will unfortunately exhaust the available space using the
- least number of songs. You also need to implement some sort of backtracking to
- find the optimal solution.
-
- >Knuth (?) has proof that this is guaranteed to use less than twice
- >the minimum possible space to fit all items, and *likely* to use
- >much less than that maximum.
-
- That is also the approach to solving the knapsack problem, with the added
- copmlexity that the selected integers have to add up to _exactly_ the given
- amount. You use backtracking to get out of impossible situations, so in effect
- you are traversing a tree of choices at the root of which you have the decision
- of including or not including the largest integers. The general solution is
- NP-complete, but for certain sequences of integers, the solution is trivial.
- Such sequences are ones in which the next largest item is bigger than the sum
- of the previous items. The simplest such series is the powers of two.
-
- 2 > 1
- 4 > 2 + 1
- 8 > 4 + 2 + 1
-
- etc.
-
- In such a series, if you _can_ include the largest number in the sum, you
- simply include and forget about it. No backtracking is needed and you reach the
- leaf of the decision tree in O(log n) time.
-
- Suppose that some combination or combinations of the songs will cover the tape
- exactly. Then a search for these optimal solutions is the same as a search for
- the solution to the knapsack problem. Simply choosing the largest song that
- will fit into the remaining space, without allowing for backtracking, will not
- find the general solution for this special case which we know is equivalent to
- the knapsack problem; since it doesn't solve some instances of the special
- case optimally, it cannot be a general solution.
-
-
-
- --
-
-